<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/15
 * Time: 8:35
 */

namespace app\admin\controller;


use app\admin\model\AuthRule;

class RuleController extends BaseController
{
    public function index()
    {
        $this->assign([
           'list' => AuthRule::all(),
        ]);
        return $this->fetch();
    }

    public function add()
    {
        if($this->request->isPost()){
            $data = input('post.');
            $result = $this->validate($data,'Rule');
            if(true !== $result){
                // 验证失败 输出错误信息
                return array('status' => 0,'msg' => $result,'icon' => 2);
            }
            $AuthRule = new AuthRule($data);
            $AuthRule->allowField(true)->save();
            return array('status' => 1,'msg' => '添加成功','icon' => 1);
        }
        return $this->fetch();
    }

    public function edi()
    {
        if($this->request->isPost()) {
            $data = input('post.');
            $result = $this->validate($data,'Rule');
            if(true !== $result){
                // 验证失败 输出错误信息
                return array('status' => 0,'msg' => $result,'icon' => 2);
            }
            $AuthRule = new AuthRule;
            $AuthRule->allowField(true)->isUpdate(true)->update($data);
            return array('status' => 1,'msg' => '修改成功','icon' => 1);
        }
        $this->assign([
            'getOneID' => AuthRule::get(input('id/d')),
        ]);
        return $this->fetch();
    }

    public function del()
    {
        AuthRule::destroy(input('id/d'));
        return array('status' => 1,'msg' => '删除成功','icon' => 1);
    }

    public function change()
    {
        $info = AuthRule::get(input('id/d'));
        if($info['status'] == 1){
            $info->save(['status' => 0]);
            return array('msg' => '已禁用','status' => 1,'icon' => 1);
        }else{
            $info->save(['status' => 1]);
            return array('msg' => '已启用','status' => 1,'icon' => 1);
        }
    }

    public function change_title()
    {
        $data = input('post.');
        $authRule = new AuthRule;
        $authRule->id = $data['id'];
        $authRule->title = $data['title'];
        $authRule->isUpdate(true)->save();
        return array('msg' => '修改成功','status' => 1,'icon' => 1);
    }
}